package com.example.demo.modules.customDefine.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.modules.customDefine.entity.FieldType;
import com.example.demo.modules.customDefine.entity.ModuleField;
import com.example.demo.modules.customDefine.mapper.FieldTypeMapper;
import com.example.demo.modules.customDefine.mapper.ModuleFieldMapper;
import com.example.demo.modules.customDefine.service.ModuleFieldService;
import com.example.demo.modules.customDefine.vo.ModuleFieldVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ModuleFieldServiceImpl extends ServiceImpl<ModuleFieldMapper, ModuleField> implements ModuleFieldService {

    @Autowired
    private ModuleFieldMapper moduleFieldMapper;

    @Autowired
    private FieldTypeMapper fieldTypeMapper;

    //查询所有
    @Override
    public List<ModuleField> listAll(QueryWrapper queryWrapper) {
        List<ModuleField> moduleFields = this.list(queryWrapper);
        return moduleFields;
    }

    /*
    * @flow
    * 1.field_type =>添加字段 field_type_name
    * */
    @Override
    public List<ModuleFieldVO> listAllWithFieldTypeName(QueryWrapper queryWrapper ) {
        List<ModuleField> moduleFields = this.list(queryWrapper);
        List<ModuleFieldVO> moduleFieldVOs = new ArrayList<>();
        for (ModuleField moduleField : moduleFields) {
            ModuleFieldVO moduleFieldVO = new ModuleFieldVO();
            FieldType fieldType = fieldTypeMapper.selectById(moduleField.getId());
            BeanUtils.copyProperties(moduleField,moduleFieldVO);
            moduleFieldVO.setFieldTypeName(fieldType.getDescription());
            moduleFieldVOs.add(moduleFieldVO);
        }

        return moduleFieldVOs;
    }

    //条件查询
    @Override
    public ModuleField listOne(Long id) {
        ModuleField moduleField = this.getById(id);
        return moduleField;
    }

    //保存
    @Override
    public int insert(ModuleField moduleField){
        return moduleFieldMapper.insert(moduleField);

    }

}